#include <Arduino.h>
#include <Wire.h>
#include "SSD1306Wire.h"
#include "HX711.h"
#include "DS18B20.h"
//屏幕初始化
SSD1306Wire display(0x3c, SDA, SCL); // ADDRESS, SDA, SCL
DS18B20  ds(25,1);

//称重初始化
#define Weight_raw 68400
#define Weight_factor 210
HX711 scale;
const int LOADCELL_DOUT_PIN = 14;
const int LOADCELL_SCK_PIN = 27;

int now_data = Weight_raw;

void TouchEvent()
{
  long raw_data = scale.read_average(1);
  now_data=-raw_data;
}

void setup()
{
  //-1.屏幕初始化
  display.init();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_16);
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  //-2.称重初始化
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  //-3.清零初始化
  touchAttachInterrupt(T0, TouchEvent, 40);
}

void loop()
{
  // clear the display
  display.clear();
  //读取原始数据
  long time1=micros();
  long raw_data = scale.read_average(1); //以一次平均值读取
  long time2=micros();
  float data = (-raw_data - now_data); //原始数据-校准数据
  display.drawString(0, 0, "Mode :");
  display.drawString(60, 0, String(time2-time1));
  // display.drawString(50, 0, String(scale.read()));
  display.drawString(0, 20, "weight :");
  // display.drawString(60, 20, String(data / Weight_factor));      //*两位小数：校准数据/称重因子=重量
  display.drawString(60, 20, String((int)data / Weight_factor)); //*整数：校准数据/称重因子=重量
  // display.drawString(0, 40, String(now_data));
  display.drawString(0, 40, "Tempu :");
  ds.start(0);
  display.drawString(60, 40, String(ds.get(0)));

  display.display();
}
